//
//  RAFSUDetectInfoView.m
//  RAFSUManagement
//
//  Created by Piccolo on 23/03/2018.
//  Copyright © 2018 Rayonics. All rights reserved.
//

#import "RAFSUDetectInfoView.h"

@interface RAFSUDetectInfoView ()

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;
@property (weak, nonatomic) IBOutlet UILabel *label5;
@property (weak, nonatomic) IBOutlet UILabel *label6;
@property (weak, nonatomic) IBOutlet UILabel *label7;
@property (weak, nonatomic) IBOutlet UILabel *label8;
@property (weak, nonatomic) IBOutlet UILabel *label9;
@property (weak, nonatomic) IBOutlet UILabel *label10;
@property (weak, nonatomic) IBOutlet UILabel *label11;



@end

@implementation RAFSUDetectInfoView

- (void)awakeFromNib {
    
    [super awakeFromNib];
}

- (void)updateViewFromDetectInfo:(RAFSUSDKDetectInfo *)detectInfo deviceID:(NSString *)deviceID macAddress:(NSString *)macAddress {
    
    _label1.text = [self stringFromKey:@"温度" value:[NSString stringWithFormat:@"%.2f℃", detectInfo.fsuTemp]];
    _label2.text = [self stringFromKey:@"电量" value:[NSString stringWithFormat:@"%ld%%", (long)detectInfo.fsuBattery]];
    _label3.text = [self stringFromKey:@"震动" value:[NSString stringWithFormat:@"%@", detectInfo.fsuShaking ? @"不正常" : @"正常"]];
    _label4.text = [self stringFromKey:@"事件" value:[NSString stringWithFormat:@"%ld", (long)detectInfo.historyEventCount]];
    for (RAFSUSDKLockInfo *fsuLock in detectInfo.locksArray) {
        NSInteger index = [detectInfo.locksArray indexOfObject:fsuLock];
        switch (index) {
            case 0:
                _label5.text = [self contactInfoStringFromLock:fsuLock];
                _label5.textColor = [self lockStatusColorFromType:fsuLock.contactStatus];;
                _label7.text = [self cylinderInfoStringFromLock:fsuLock];
                _label7.textColor = [self lockStatusColorFromType:fsuLock.cylinderStatus];;
                break;
            case 1:
                _label6.text = [self contactInfoStringFromLock:fsuLock];
                _label6.textColor = [self lockStatusColorFromType:fsuLock.contactStatus];;
                _label8.text = [self cylinderInfoStringFromLock:fsuLock];
                _label8.textColor = [self lockStatusColorFromType:fsuLock.cylinderStatus];;
                break;
                
            default:
                break;
        }
    }
    _label9.text = [self stringFromKey:@"时间" value:detectInfo.fsuTime];
    _label10.text = [self stringFromKey:@"设备ID" value:deviceID];
    _label11.text = [self stringFromKey:@"Mac地址" value:macAddress];
    [RAUtil setSuperView:self fromLastSubView:_label11 spacing:INNER_BOUNDS_SPACING];
    
}

- (void)clean {
    
    _label1.text = @"温度:";
    _label2.text = @"电量:";
    _label3.text = @"震动:";
    _label4.text = @"事件:";
    _label5.text = @"锁1门磁:";
    _label6.text = @"锁2门磁:";
    _label7.text = @"锁1门锁:";
    _label8.text = @"锁2门锁:";
    _label9.text = @"时间:";
    _label10.text = @"设备ID:";
    _label11.text = @"Mac地址:";
}

- (NSString *)lockStatusStringFromType:(NSRAFSUSDKLockStatusType)type {
    
    switch (type) {
        case NSRAFSUSDKLockStatusTypeNull:
            return @"未知";
        case NSRAFSUSDKLockStatusTypeLocked:
            return @"关闭🔒";
        case NSRAFSUSDKLockStatusTypeUnlocked:
            return @"打开🔓";
        default:
            return @"未知";
    }
}

- (UIColor *)lockStatusColorFromType:(NSRAFSUSDKLockStatusType)type {
    
    switch (type) {
        case NSRAFSUSDKLockStatusTypeNull:
            return [UIColor blackColor];
        case NSRAFSUSDKLockStatusTypeLocked:
            return [UIColor redColor];
        case NSRAFSUSDKLockStatusTypeUnlocked:
            return [UIColor greenColor];
        default:
            return [UIColor blackColor];
    }
}

- (NSString *)contactInfoStringFromLock:(RAFSUSDKLockInfo *)fsuLock {
    
    return [self stringFromKey:[NSString stringWithFormat:@"锁%ld门磁", (long)fsuLock.serialNumber] value:[NSString stringWithFormat:@"%@", [self lockStatusStringFromType:fsuLock.contactStatus]]];
}

- (NSString *)cylinderInfoStringFromLock:(RAFSUSDKLockInfo *)fsuLock {
    
    return [self stringFromKey:[NSString stringWithFormat:@"锁%ld门锁", (long)fsuLock.serialNumber] value:[NSString stringWithFormat:@"%@", [self lockStatusStringFromType:fsuLock.cylinderStatus]]];
}


- (NSString *)stringFromKey:(NSString *)key value:(NSString *)value {
    
    NSString *string = [NSString stringWithFormat:@"%@:%@", key, value];
    return string;
}
@end
